package com.mengtian_tech.tju.dataquery.api.controller;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mengtian_tech.tju.dataquery.api.model.ClientType;
import com.mengtian_tech.tju.dataquery.api.model.dataAIMysql.LoginSession;
import com.mengtian_tech.tju.dataquery.api.service.EmployeeService;
import com.mengtian_tech.tju.dataquery.api.service.LoginSessionService;
import com.mengtian_tech.tju.dataquery.api.util.SessionUtil;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 移动端入口地址。
 * 未登录时，cas filter会拦截，cas登录成功后跳转到此路径 /login，然后返回前端页面地址
 * 已经登录时，不再拦截，直接进入控制器下的“/”，进入后返回前端web地址页面
 */
@Slf4j
@Controller
@RequestMapping("/")
public class IndexController {
	@Autowired
	LoginSessionService loginSessionService;
	@Autowired
	EmployeeService employeeService;

	@Autowired
	private Environment env;
	 
    @SneakyThrows
    @GetMapping(value = "/login")
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //只是为了跳转到cas服务端的登录页面  登录成功后会跳回此页
        //可根据自身系统进行代码增强
    	
    	//判断一下session，如果是PC端过来的，则重定位到PC端
    	if(ClientType.CLIENT_WEB.equals(SessionUtil.getClientType())){
    		redirectToPCPage(request,response);
    	}else {
    		redirectToAppPage(request, response);
    	}
    	
    }

	
    
    /**
     * 已经登录，第二次调用直接会进入此页面
     * @param request
     * @param response
     * @throws IOException
     */
    @SneakyThrows
    @GetMapping(value = "/")
    public void defaultLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //只是为了跳转到cas服务端的登录页面  登录成功后会跳回此页
        //可根据自身系统进行代码增强
    	
    	//判断一下session，如果是PC端过来的，则重定位到PC端
    	if(ClientType.CLIENT_WEB.equals(SessionUtil.getClientType())){
    		redirectToPCPage(request,response);
    	}else {
    		redirectToAppPage(request, response);
    	}
    	
    	
    }
    
    /**
     * 登录成功后，如果session中存放了登陆前地址，则重定向到登录之前的地址，否则返回默认地址
     * @param response
     */
    private void redirectToAppPage(HttpServletRequest req, HttpServletResponse response) throws IOException {
		String siteUrl = env.getProperty("app.webUrl");
		String callback = SessionUtil.getCallbackURL();
		String redirectURL = "";
		
		System.out.println("get callback = "+callback);
		
		if(StringUtils.isEmpty(callback)) {
			redirectURL = siteUrl;
		}else {
			redirectURL = callback;
			SessionUtil.setCallbackURL(null);
		}
		

		//记录登录log
		loginSuccess(req.getRemoteUser(),req.getRemoteAddr(),redirectURL,ClientType.CLIENT_MOBILE);
		
		response.sendRedirect(redirectURL);
	}
    
    /**
     * 登录成功后，如果session中存放了登陆前地址，则重定向到登录之前的地址，否则返回默认地址
     * @param response
     */
    private void redirectToPCPage(HttpServletRequest req, HttpServletResponse response) throws IOException {
		String siteUrl = env.getProperty("app.PCUrl");
		String callback = SessionUtil.getCallbackURL();
		String redirectURL = "";
		
		System.out.println("get callback = "+callback);
		
		if(StringUtils.isEmpty(callback)) {
			redirectURL = siteUrl;
		}else {
			redirectURL = callback;
			SessionUtil.setCallbackURL(null);
		}
		

		//记录登录log
		loginSuccess(req.getRemoteUser(),req.getRemoteAddr(),redirectURL,ClientType.CLIENT_WEB);
		
		response.sendRedirect(redirectURL);
	}
    
    private void loginSuccess(String staffId,String ip,String remark,int clientType) {
    	SessionUtil.setUser(staffId);
    	SessionUtil.setClientType(clientType);
    	
    	LoginSession session = this.loginSessionService.createSession(staffId, ip,remark,clientType);
    	SessionUtil.setSessionId(session.getId());
    }

}
